/* 头文件 */
#include <linux/init.h>
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/kdev_t.h>
#include <linux/kernel.h>
/* 参数传入 */
static int major;
static int minor;
module_param(major, int, S_IRUGO);
module_param(minor, int, S_IRUGO);

static dev_t dev_num;

/* 符号导出 */

/* 函数 */
static int dev_t_init(void)
{
    int ret;
    if(major)
    {
        dev_num = MKDEV(major, minor);
        printk(KERN_EMERG "major is %d\r\n", major);
        printk(KERN_EMERG "minor is %d\r\n", minor);

        ret = register_chrdev_region(dev_num, 1, "chrdev_name");
        if(ret<0)
        {
            printk(KERN_EMERG "register_chrdev_region is error\r\n");
        }
        printk(KERN_EMERG "register_chrdev_region is ok\r\n");
    }
    else
    {
        ret = alloc_chrdev_region(&dev_num, 0, 1, "chrdev_num");
        if(ret<0)
        {
            printk(KERN_EMERG "register_chrdev_region is error\r\n");
        }
        printk(KERN_EMERG "register_chrdev_region is ok\r\n");

        major = MAJOR(dev_num);
        minor = MINOR(dev_num);
        printk(KERN_EMERG "major is %d\r\n", major);
        printk(KERN_EMERG "minor is %d\r\n", minor);
    }
    return 0;
}

static void dev_t_exit(void)
{
    /* 释放字符驱动设备 */
    unregister_chrdev_region(dev_num, 1);

    printk(KERN_EMERG "module exit \n");
}
/* 模块加载 */
module_init(dev_t_init);

/* 模块卸载 */
module_exit(dev_t_exit);

/* 开源协议 */
MODULE_LICENSE("GPL v2");
/* 作者信息 */
MODULE_AUTHOR("jianghe");
